package com.prone.vyuan.widget.dpdialog;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.dongdongsongcan.app.R;

/**
 * 限定高度的DialogContent
 * 
 * @author Prone
 */
public class DialogContentFrameLayout extends FrameLayout {

	private int mMaxHeight = 0;

	public DialogContentFrameLayout(Context context) {
		super(context);
		init(context);
	}

	public DialogContentFrameLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public DialogContentFrameLayout(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context ctx) {
		Resources res = ctx.getResources();
		int height = res.getDisplayMetrics().heightPixels;
		int dialogTitleHeight = 47;
		int margin = res.getDimensionPixelSize(R.dimen.dialog_min_margin);
		int dialogBottomButtonHeight = res
				.getDimensionPixelSize(R.dimen.dialog_title_text_height);

		mMaxHeight = height - dialogTitleHeight - dialogBottomButtonHeight
				- margin;
	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int height = getMeasuredHeight();
		int specWidthSize = MeasureSpec.getSize(widthMeasureSpec);
		if (height > mMaxHeight) {
			setMeasuredDimension(specWidthSize, mMaxHeight);
		} else {
			setMeasuredDimension(specWidthSize, height);
		}
	}

}
